window.onload = function () {
    var allA = document.getElementsByTagName("a");
    var submit = document.getElementById("button");


    //定义一个删除函数
    function del() {
        var tr = this.parentNode.parentNode;
        //var name = tr.getElementsByTagName("td")[0].innerHTML;    或者使用tr的子元素获取
        var name = tr.children[0].innerHTML;
        console.log(name);
        var flag = confirm("确认删除" + name + "吗？");
        if (flag) {
            tr.parentNode.removeChild(tr);
        }
    }
    //console.log(allA);
    //为每个超链接绑定点击函数
    for (var i = 0; i < allA.length; i++) {
        allA[i].onclick = del;
    }


    //创建添加函数
    submit.onclick = function () {
        var name = document.getElementById("empName").value;
        var email = document.getElementById("email").value;
        var salary = document.getElementById("salary").value;

        if (name != 0 && email != 0 && salary != 0) {
            /*   <tr>
            <td>Tom</td>
            <td>tom@tom.com</td>
            <td>5000</td>
            <td><a href="javascript;:">Delete</a></td>
            </tr>
        */
            //按照上述样子创建一个table
            var tr = document.createElement("tr");

            tr.innerHTML = "<td>" + name + "</td>" +
                "<td>" + email + "</td>" +
                "<td>" + salary + "</td>" +
                "<td><a href='javascript:;'>Delete</a></td>";

            tr.getElementsByTagName("a")[0].onclick = del;
            /*var nameTd = document.createElement("td");
            var emailTd = document.createElement("td");
            var salaryTd = document.createElement("td");
            var aTd = document.createElement("td");
    
            //创建一个a元素
            var a = document.createElement("a");
    
    
            //创建文本节点
            var nameText = document.createTextNode(name);
            var emailText = document.createTextNode(email);
            var salaryText = document.createTextNode(salary);
            var aText = document.createTextNode("Delete");
    
            //将文本节点添加到td中
    
            nameTd.appendChild(nameText);
            emailTd.appendChild(emailText);
            salaryTd.appendChild(salaryText);
            a.appendChild(aText);
            aTd.appendChild(a);
            a.href = "javascript:;";
    
            //将td添加到tr中
            tr.appendChild(nameTd);
            tr.appendChild(emailTd);
            tr.appendChild(salaryTd);
            tr.appendChild(aTd);
            */
            var tbody = document.getElementsByTagName("tbody")[0];

            //var employee = document.getElementById("employee");
            tbody.appendChild(tr);
        } else {
            alert("数据不能为空,请重新输入！");
        }



    };

};